/*
 *
 * Copyright (C) 2008 Ed Hong
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#ifndef CODER_H
#define CODER_H

#include "libQccPack.h"
#include <math.h>
#include "contexttree.h"
#include "SubbandBits.h"
#include "SubbandIterator.h"

// encodes exactly numbitplanes (not including sign plane)
//  if numbitplanes >0; 
// otherwise encodes numbits bits. 
//  numbits ignored if numbitplanes >0. 
// f is sorting method, send NULL for no sorting. 
int encode_subband_component( SubbandComponent *sc, int numComponents, 
			     QccENTArithmeticModel *model, 
			     QccBitBuffer *output_buffer, 
			     contexttree* ct, 
			     int numbitplanes, 
			     int numbits, 
			     int (*f)(const CoeffHeapNode *, 
				      const CoeffHeapNode *, 
				      const QccENTArithmeticModel *), 
			     int debug) ;



// decode, setting decoding limit at
//    exactly numbitplanes bitplanes, except 
//     when numbitplanes<=0, then set limit at 
//     numbits bits, 
//    except if both  numbitplanes and numbits <=0,
//    then make limit effectively unlimited. 
// will attempt to decode additionalbits number of bits, 
//   except when additionalbits==0, this means
//     decode an unlimited number of additional bits. 
// will stop decoding when one of the following happens:
//   1) no more bits in input buffer
//   2) specified overall decoding limit reached
//   3) additionalbits have been decoded since
//       last invocation of decode.  
//  note that numbits specified is always in terms
//   of number of decoded bits, not in terms of
//  numbits used from the input_buffer. 
//  use NULL for sorting function for no sorting. 

// initialize decoder, setting decoding limits. 
// does not decode any bits... 
//  return result is error condition; 0 on success.  
// bitcounter is always set to the number of bits decoded
// sofar. 
void decode_subband_component_start(SubbandComponent *sc, int numComponents, 
				   QccENTArithmeticModel *model, 
				   QccBitBuffer *input_buffer, 
				   contexttree* ct, 
			     int (*f)(const CoeffHeapNode *, 
				      const CoeffHeapNode *, 
				      const QccENTArithmeticModel *), 
				   int numbitplanes, 
				   int numbits, 
				   int *bitcounter, 
				   int debug) ;

// updates limits to ones given. 
void decode_subband_component_update_limit(
				   int numbitplanes, 
				   int numbits) ;

// attempts to decode additionalbits bits, stopping
//   when additionalbits have been decoded
// returns -1 if an error in a subroutine call happened, 
// returns 0 on successful decode of additionalbits bits, 
// returns 1 if specified limit is reached (and we did not
//             successfully decode additionalbits bits) 
// returns 2 if input_buffer is empty  (and the
//             criteria for returning 0 and 1 are not satisfied). 
int decode_subband_component_continue(int additionalbits,
				      int debug) ;


sbploc get_currentloc(void);  // return current location in decoding. 



#endif /* CODER_H */
